package com.yc.txx.mapper;

import com.yc.txx.entity.Car;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 天下行租车-车辆表-10 Mapper 接口
 * </p>
 *
 * @author baomidou
 * @since 2024-05-07
 */
public interface CarMapper extends BaseMapper<Car> {

    @Select("<script>" +
            "SELECT * FROM txx_car" +
            "<where>" +
            "<if test='brand != null and brand != &quot;&quot;'>brand = #{brand} AND </if>" +
            "<if test='model != null and model != &quot;&quot;'>model LIKE CONCAT('%',#{model},'%') AND </if>" +
            "<choose>" +
            "<when test='price == 1'>price BETWEEN 0 AND 100</when>" +
            "<when test='price == 2'>price BETWEEN 101 AND 200</when>" +
            "<when test='price == 3'>price BETWEEN 201 AND 500</when>" +
            "<otherwise>price &gt; 500</otherwise>" +
            "</choose>" +
            "</where>" +
            "<if test='sort != null and sort != &quot;&quot;'>ORDER BY ${sort} </if>" +
            "<if test='sort != null and sort != &quot;&quot; and sc != null and sc != &quot;&quot;'>${sc} </if>" +
            "</script>")
    List<Car> select( Car car);

    @Select("select distinct brand from txx_car")
    public List<String> selectBrand();

    @Select("select * from txx_car where id=#{id}")
    //在xml中 是 <resultMap><result><assi...>
    //<resultMap>= @Results    @Result=<result>  <assi...>=@One
    @Results(
            id="rm1", /*这个随便*/
            value={
                    //id组件映射 =》提升缓存性能 ，建议配置
                    @Result(column = "id",property = "id" ,id=true),
                    @Result(column = "brand" ,property = "brand"),
                    //一对多
                    @Result(column = "id", property="photo",many = @Many(select = "com.yc.txx.mapper.ImageMapper.getDetailImg"))
            }
    )
    public Car selectCarDetail(String id);
}
